package com.bsg.upm.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bsg.upm.exception.APIException;
import com.bsg.upm.http.RespJson;
import com.bsg.upm.http.RespJsonFactory;
import com.bsg.upm.service.BusinessSystemService;

/**
 * 业务系统理控制层
 * 
 * @author HCK
 *
 */
@Controller
@RequestMapping("/v1.0/business_systems")
public class BusinessSystemController {

	@Resource
	private BusinessSystemService businessSystemService;

	/**
	 * 【查询】
	 * 
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public RespJson listBusinessSystem(@RequestParam Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			Map<String, Object> searchParamMap = new HashMap<String, Object>();
			respJson = businessSystemService.list(searchParamMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【详情】
	 * 
	 * @param businessSystemId
	 *            业务系统编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{business_system}", method = RequestMethod.GET)
	@ResponseBody
	public RespJson getBusinessSystem(@PathVariable("business_system") String businessSystemId,
			HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = businessSystemService.get(businessSystemId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【新增】
	 * 
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public RespJson saveBusinessSystem(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = businessSystemService.save(paramMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【编辑】
	 * 
	 * @param businessSystemId
	 *            业务系统编码
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{business_system}", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson updateBusinessSystem(@PathVariable("business_system") String businessSystemId,
			@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			paramMap.put("businessSystemId", businessSystemId);
			respJson = businessSystemService.update(paramMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【删除】
	 * 
	 * @param businessSystemId
	 *            业务系统编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{business_system}", method = RequestMethod.DELETE)
	@ResponseBody
	public RespJson removeBusinessSystem(@PathVariable("business_system") String businessSystemId,
			HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = businessSystemService.remove(businessSystemId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}
}
